import {timeFormat} from '../../utils/utils'
let dateTimePicker = require('../../utils/dateTimePicker.js');
import Dialog from '@vant/weapp/dialog/dialog';
const app = getApp()
import Toast from '@vant/weapp/toast/toast'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isAshow:false,
    isVIPno:false,
    userinfo:{},
    violateValue:'',
    violateIndex:0,
    type_id:'',
    violateState:[{
      id: 0,
      text: '选择类型',
    }
  ],
  fileList: [],
  fileqrcodeList:[],
    minHour: 10,
    maxHour: 20,
    currentDate: '',
    currentaddress:'',
    licenseNumber:'',
    dateTimeArray1: null,
    dateTime1: null,
    startYear: null,
    endYear: null,
  },
  getconfirmvip(){
    this.setData({
      isVIPno:false
    })
    wx.setStorageSync('isVIPno', 1)
  },
  submit(){
    let that = this
    if(that.data.userinfo.status==1){
      Toast('暂无权限，请联系客服！')
      return
    }
    let params = {}
    // params.violateValue = that.data.violateValue
    if(that.data.fileList.length==1){
      params.images = that.data.fileList[0].path
    }else if(that.data.fileList.length==0){
      params.images = ''
    }else{
      params.images = ''
      that.data.fileList.map(item=>{
        params.images+=item.path+'|'
      })
      params.images= params.images.slice(0, -1);
    }
    if(that.data.fileqrcodeList.length>0){
      params.qrcode = that.data.fileqrcodeList[0].path
    }else{
      params.qrcode = ''
    }
    params.wf_time = that.data.currentDate
    params.wf_address = that.data.currentaddress
    params.car_number = that.data.licenseNumber
    params.type_id = Number(that.data.type_id)
    if(params.type_id==0){
      Toast.fail('类型未选择')
      return
    }
    if(that.data.fileList.length<3){
      Toast.fail('需三张照片')
      return
    }
    if(params.wf_time==''){
      Toast.fail('时间未选择')
      return
    }
    if(params.wf_address==''){
      Toast.fail('路段未填写')
      return
    }
    if(params.car_number==''){
      Toast.fail('车牌号未填写')
      return
    }
    if(params.qrcode==''){
      Toast.fail('收款方式未上传')
      return
    }
     Toast.loading({
      duration: 0, // 持续展示 toast
      message: '提交中',
      mask:true
    });
    app.globalData.header.Authorization = wx.getStorageSync('token')
    wx.request({
      url:app.serverUrl+ '/api/wforder',
      header:app.globalData.header,
      method: 'POST',
      data:params,
      success:res=>{
        if(res.data.code==200){
          Toast.success('已提交')
          setTimeout(()=>{
            wx.navigateBack({
              delta: 1
            })
          },2000)
        }else{
          Toast({
            message:res.data.message[0]
          })
        }
      }
    })
  },
  onLicense(event){
    let that = this 
    that.setData({
      licenseNumber:event.detail
    })
  },
  onAddress(event){
    let that = this 
    that.setData({
      currentaddress:event.detail
    })
  },
  
  deleteUP(event){
    let that = this
    let newarrlist = that.data.fileList
    newarrlist.splice(event.detail.index,1)
    that.setData({
      fileList:newarrlist
    })
  },
  
  afterRead(event) {
    let that = this
    const { file } = event.detail;
    // console.log(file);
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: app.serverUrl+'/api/upload', 
      name: 'file',
      filePath: file.tempFilePath,
      method: 'POST',
      header:{
        "Authorization":wx.getStorageSync('token'),
        "X-Requested-With":"XMLHttpRequest",
        "Content-Type":"application/json"
      },
      success(res) {
       res = JSON.parse(res.data)
        // 上传完成需要更新 fileList
       if(res.code==200){
         let fileList = that.data.fileList
          fileList.push(res.data)
          that.setData({
            fileList
          })
       }
      },
    });
  },
  deleteqrcode(event){
    let that = this
    let newarrlist = that.data.fileqrcodeList
    newarrlist.splice(event.detail.index,1)
    that.setData({
      fileqrcodeList:newarrlist
    })
  },
  qrcodeafterRead(event) {
    let that = this
    const { file } = event.detail;
    // console.log(file);
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: app.serverUrl+'/api/upload', 
      name: 'file',
      filePath: file.tempFilePath,
      method: 'POST',
      header:{
        "Authorization":wx.getStorageSync('token'),
        "X-Requested-With":"XMLHttpRequest",
        "Content-Type":"application/json"
      },
      success(res) {
       res = JSON.parse(res.data)
        // 上传完成需要更新 fileqrcodeList
       if(res.code==200){
         let fileqrcodeList = that.data.fileqrcodeList
          fileqrcodeList.push(res.data)
          that.setData({
            fileqrcodeList
          })
       }
      },
    });
  },
  onViolate(){
    let that = this
    that.setData({
      isAshow:true
    })
  },
  cancelpicker(e) {
    let that = this
    that.setData({
      isAshow: false
    })
  },
 
  onClose(){
    let that = this
    that.setData({
      isAshow:false
    })
  },
  onConfirmViolateState(event) {
    let that = this
    that.setData({
      violateIndex:event.detail.index,
      violateValue: event.detail.value.id == 0 ? '' : event.detail.value.text,
      type_id:event.detail.value.id ,
      isAshow: false
    })
  },
  getgenre(){
    let that = this
    if(that.data.violateState.length>1)return
    app.globalData.header.Authorization = wx.getStorageSync('token')
    wx.request({
      url: app.serverUrl + '/api/wftype',
      header:app.globalData.header,
      method: 'GET',
      success: function (res) {
        if(res.data.code==200){
          let arraynew = [{
            id: 0,
            text: '选择类型',
          }]
          for(let obj in res.data.data){
            let objectdiy = {}
            objectdiy.id = obj
            objectdiy.text = res.data.data[obj]
            arraynew.push(objectdiy)
          }
          that.setData({
            violateState:arraynew
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this;
    that.setData({
      userinfo:JSON.parse(options.userinfo)
    })
    that.fundateinit()
  },
  fundateinit(){
    let that = this 
     // 获取完整的年月日 时分秒，以及默认显示的数组
     var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
     var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
     // 精确到分的处理，将数组的秒去掉
     //var lastArray = obj1.dateTimeArray.pop();
     //var lastTime = obj1.dateTime.pop();
 
     this.setData({
       dateTimeArray1: obj1.dateTimeArray,
       dateTime1: obj1.dateTime
     });
     wx.setStorageSync('dates',obj1.dateTime)
  },
  cancelHandling(e){
    let that = this
    if(!this.data.currentDate){
      that.fundateinit()
    }
    this.setData({
      dateTime1:wx.getStorageSync('dates')
    })
  },
// 选择日期时间
changeDateTime1(e) {
  let that = this
  that.setData({
    dateTime1: e.detail.value,
    currentDate:that.dateEndfun(that.data.dateTimeArray1,e.detail.value)
  });
  wx.setStorageSync('dates',e.detail.value)
},
dateEndfun(data,index){
  let str = ''
  str = data[0][index[0]]+'-'+data[1][index[1]]+'-'+data[2][index[2]]+' '+data[3][index[3]]+':'+data[4][index[4]]+':'+data[5][index[5]]
  return str
},
changeDateTimeColumn1(e) {
  var arr = this.data.dateTime1,
    dateArr = this.data.dateTimeArray1;
  arr[e.detail.column] = e.detail.value;
  dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
  this.setData({
    dateTimeArray1: dateArr,
    dateTime1: arr
  });
},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getgenre()
    if(this.data.userinfo.status==1){
      this.setData({
        isVIPno:true
      })
    }else if(wx.getStorageSync('isVIPno')!=1){
      this.setData({
        isVIPno:true
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    wx.removeStorageSync('dates')
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})